package Thread;

import java.util.concurrent.locks.ReentrantLock;

/*
    线程的通信

   wait() notify() notifyAll()
 */
public class CommunicationTest {
    public static void main(String[] args) {
        swap s = new swap();
        Thread t1 = new Thread(s);
        Thread t2 = new Thread(s);
         t1.setName("线程1");
         t2.setName("线程2");
         t1.start();
         t2.start();
    }
}

class swap implements Runnable {
    private int NUMBER = 1;
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {

            synchronized (this) {
                 notify();//this.notify()
                if (NUMBER <= 100) {
                    System.out.println(Thread.currentThread().getName() + ":" + NUMBER);
                    NUMBER++;
                    try {
                        wait();//this.wait()
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    break;
                }
            }

        }
    }
}
